package com.xmad.ksadutils.AdSDK.AdMold.Pangolin;


import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.xmad.ksadutils.AdSDK.AdBase.AdBaseLoad;
import com.xmad.ksadutils.AdSDK.AdBase.AdCode;
import com.xmad.ksadutils.AdSDK.AdBase.TTAdManagerHolder;
import com.xmad.ksadutils.AdSDK.Utlis.RxLogUtils;

public class PGloadRewardVideoAd extends AdBaseLoad {
    private volatile static PGloadRewardVideoAd mInstance;
    TTRewardVideoAd ttRewardVideoAd;
    /**
     * 单例模式
     *
     * @return
     */
    public static PGloadRewardVideoAd getInstance() {
        if (mInstance == null) {
            synchronized (PGloadRewardVideoAd.class) {
                if (mInstance == null) {
                    mInstance = new PGloadRewardVideoAd();
                }
            }
        }
        return mInstance;
    }


    @Override
    public void Show(final Activity activity, FrameLayout frameLayout) {
        if (ttRewardVideoAd == null) {
            if (AdCode.getKsLoadRewardVideoId().equals("") && PGAdId.equals("")) {
                RxLogUtils.e("还未设置ID");
                return;
            }
            TTAdManager ttAdManager = TTAdManagerHolder.get();
            TTAdNative TTAdNative = ttAdManager.createAdNative(activity);
            if (ToastCode){
                Toast.makeText(activity,!PGAdId.equals("") ? PGAdId: AdCode.getLoadRewardVideoId(), Toast.LENGTH_SHORT).show();
            }
            AdSlot adSlot = new AdSlot.Builder()
                    .setCodeId(!PGAdId.equals("") ? PGAdId : AdCode.getLoadRewardVideoId())
                   // .setRewardName("金币") //奖励的名称 选填
                   // .setRewardAmount(3)  //奖励的数量 选填
                    .setUserID("tag123")//tag_id
                    .setMediaExtra("media_extra") //附加参数
                    .setOrientation(TTAdConstant.VERTICAL) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                    .build();
            TTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
                @Override
                public void onError(int code, String msg) {
                    RxLogUtils.e("Callback --> onError: " + code + ", " + String.valueOf(msg));
                    if (listener != null) {
                        listener.onError(code+msg);
                    }
                }

                @Override
                public void onRewardVideoAdLoad(final TTRewardVideoAd ttRewardVideoAd) {
                    RxLogUtils.e("onRewardVideoAdLoad");
                    if (ttRewardVideoAd != null) {
                        //展示广告，并传入广告展示的场景
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                ttRewardVideoAd.showRewardVideoAd(activity);
                            }
                        });
                    } else {
                        RxLogUtils.e("请先加载广告");
                    }
                    ttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
                        //广告的下载bar点击回调
                        @Override
                        public void onAdVideoBarClick() {

                        }

                        //视频广告关闭回调
                        @Override
                        public void onAdClose() {
                            if (listener != null) {
                                listener.onPageDismiss();
                            }
                            RxLogUtils.e("onAdClose");
                            PGAdId = "";
                        }

                        //视频播放完成回调
                        @Override
                        public void onVideoComplete() {
                            if (listener != null) {
                                listener.onComplete();
                            }
                            RxLogUtils.e("onVideoComplete");
                        }

                        //视频广告播放错误回调
                        @Override
                        public void onVideoError() {
                            if (listener != null) {
                                listener.onError("onVideoError");
                                RxLogUtils.e("onVideoError");
                            }
                        }

                        //视频播放完成后，奖励验证回调，rewardVerify：是否有效，rewardAmount：奖励梳理，rewardName：奖励名称，code：错误码，msg：错误信息
                        @Override
                        public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName, int code, String msg) {

                        }

                        //视频广告跳过回调
                        @Override
                        public void onSkippedVideo() {
                            RxLogUtils.e("onSkippedVideo");
                            if (listener != null) {
                                listener.onJump(200);
                            }
                        }

                        //视频广告展示回调
                        @Override
                        public void onAdShow() {
                            RxLogUtils.e("onAdShow");
                            if (listener != null) {
                                listener.onAdShow();
                            }
                        }
                    });

                }

                //视频广告加载后，视频资源缓存到本地的回调，在此回调后，播放本地视频，流畅不阻塞。
                @Override
                public void onRewardVideoCached() {

                }

                @Override
                public void onRewardVideoCached(TTRewardVideoAd mttRewardVideoAd) {

                }
            });
        }
    }


}
